package com.adb.indian.rto.actionbarcompat;

/*
 * Copyright 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.adb.indian.rto.R;

/**
 * A base activity that defers common functionality across app activities to an {@link
 * ActionBarHelper}.
 *
 * NOTE: dynamically marking menu items as invisible/visible is not currently supported.
 *
 * NOTE: this may used with the Android Compatibility Package by extending
 * android.support.v4.app.FragmentActivity instead of {@link Activity}.
 */
public abstract class ActionBarActivity extends FragmentActivity {
    final ActionBarHelper mActionBarHelper = ActionBarHelper.createInstance(this);
    
    boolean isShowWindowTitleBar = true;
    boolean isShowWindowFullScreen  = false;
    boolean isActionAndTitleBar = false;
    
    /**
     * Returns the {@link ActionBarHelper} for this activity.
     */
    protected ActionBarHelper getActionBarHelper() {
        return mActionBarHelper;
    }

    /**{@inheritDoc}*/
    @Override
    public MenuInflater getMenuInflater() {
        return mActionBarHelper.getMenuInflater(super.getMenuInflater());
    }

    /**{@inheritDoc}*/
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActionBarHelper.onCreate(savedInstanceState);
    }

    /**{@inheritDoc}*/
   
	@Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mActionBarHelper.onPostCreate(savedInstanceState);
        
        //hide title text in both ice and honey come and add out custom view with our font styles 
        // if device is not ics and honey comb then no need to change 
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
	    	//if ICE_CREAM_SANDWICH or later version running
        	addCustomTitleInActionBar();
        	
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        	// if HONEYCOMB or later version is running
        	addCustomTitleInActionBar();
         }
    }

    /**
     * Base action bar-aware implementation for
     * {@link Activity#onCreateOptionsMenu(android.view.Menu)}.
     *
     * Note: marking menu items as invisible/visible is not currently supported.
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean retValue = false;
        retValue |= mActionBarHelper.onCreateOptionsMenu(menu);
        retValue |= super.onCreateOptionsMenu(menu);
        return retValue;
    }

    /**{@inheritDoc}*/
    @SuppressLint("NewApi")
	@Override
    protected void onTitleChanged(CharSequence title, int color) {
        mActionBarHelper.onTitleChanged(title, color);
        super.onTitleChanged(title, color);
    }
    
    /**
     * Default is true
     * @param value
     */
  protected void showWindowTitleBar(boolean value){
    	isShowWindowTitleBar = value;
    	 if(!isShowWindowTitleBar){
         	// Not show window Title Bar or actionbar remove
    		    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    		    	//if ICE_CREAM_SANDWICH or later version running
    		    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    	        	// if HONEYCOMB or later version is running
    	        	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	         } else {
    	            //IF HONEYCOMB or Previous version no need to handle
    	        	
    	        }
         }
    }
   
    /**
     * Default is false
     * @param value
     */
	protected void showWindowFullScreen(boolean value) {
		isShowWindowFullScreen=value;
		 if(isShowWindowFullScreen){
	        	// show window Full screen
	        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
	                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
	        	
	        }
	}
	
	
	@SuppressLint({ "NewApi", "NewApi", "NewApi" })
	protected void hideActionBar(boolean value){
		isActionAndTitleBar = value;
		ActionBar mActionBar = null;
		
		if(!isActionAndTitleBar){
			
			 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			    	//if ICE_CREAM_SANDWICH or later version running
			    	mActionBar = getActionBar();
			    	if(mActionBar != null){
			    		if(!mActionBar.isShowing())
			    			mActionBar.show();
			    	}
			    	
		        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		        	// if HONEYCOMB or later version is running
		        	mActionBar = getActionBar();
		        	
		        	if(mActionBar != null){
		        		if(!mActionBar.isShowing())
			    			mActionBar.show();
		        	 }
			    		
			        	
		         } else {
		        	 
		            requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		        }
			 
			return;
		}else{
			
			 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
			    	//if ICE_CREAM_SANDWICH or later version running
			    	mActionBar = getActionBar();
			    	if(mActionBar != null){
			    		if(mActionBar.isShowing()){
			    			mActionBar.hide();
			    		}
			    		
			    	}
			    	
		        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
		        	// if HONEYCOMB or later version is running
		        	mActionBar = getActionBar();
		        	if(mActionBar != null){
		        		if(mActionBar.isShowing()){
		        			mActionBar.hide();
		        		}
		        		
		        	}
		        	
		         } else {
		            requestWindowFeature(Window.FEATURE_NO_TITLE);
		        }
		}
		
	}
	
	 @SuppressLint({ "NewApi", "NewApi" })
	private void addCustomTitleInActionBar(){
		
		getActionBar().setDisplayShowCustomEnabled(true);
		getActionBar().setDisplayShowTitleEnabled(false);
		
		LayoutInflater inflator = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View mView = inflator.inflate(R.layout.actionbar_titleview, null);

		//if you need to customize anything else about the text, do it here.
		//I'm using a custom TextView with a custom font in my layout xml so all I need to do is set title
		((TextView)mView.findViewById(R.id.iTextView)).setText(this.getTitle());
		
		getActionBar().setCustomView(mView);
	}
    
}
